package com.jmp.api.controller;

import com.jmp.api.service.WechatBaseService;
import com.jmp.api.service.WechatComponentService;
import com.jmp.base.WechatApi;
import com.jmp.base.dto.*;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class WechatController implements WechatApi {
    @Autowired
    private WechatBaseService wechatBaseService;
    @Autowired
    private WechatComponentService wechatComponentService;

    @Override
    public ResultModel<WechatTokenResponse> token(@RequestBody WechatTokenRequest request) {
        String token = wechatBaseService.getWxToken(request.getAppId(), request.getAppSecret());
        WechatTokenResponse tokenResponse = WechatTokenResponse.builder()
                .token(token)
                .build();
        return ResultModel.success(tokenResponse);
    }

    @Override
    public ResultModel<WechatTokenResponse> ticket(@RequestBody WechatTokenRequest request) {
        String ticket = wechatBaseService.getTicket(request.getAppId(), request.getAppSecret());
        WechatTokenResponse response = WechatTokenResponse.builder().token(ticket).build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<WechatSendMsgResponse> sendMsg(@RequestBody WechatSendMsgRequest request) {
        return null;
    }

    @Override
    public ResultModel templateInit(@RequestBody WechatTemplateInitRequest request) {
        wechatComponentService.templateInit(request.getCompanyId(), request.getLoginEnum(), request.getAppId());
        return ResultModel.success();
    }

    @Override
    public ResultModel<WechatComponentTokenResponse> componentToken() {
        String token = wechatComponentService.getComponentToken();
        WechatComponentTokenResponse response = WechatComponentTokenResponse.builder().token(token).build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<WechatAuthorizerTokenResponse> authorizerToken(@RequestBody WechatAuthorizerTokenRequest request) {
        String token = wechatComponentService.getAuthorizerToken(request.getAppId());
        WechatAuthorizerTokenResponse response = WechatAuthorizerTokenResponse.builder().token(token).build();
        return ResultModel.success(response);
    }
}
